<?php

class suppliers{
	function getSupplierList(){
		$supplierList = array();
		qdbconn();
		
		$query = "SELECT * FROM suppliers";
		$result = mysql_query($query);
		
		$i = 0;
		while ($row = mysql_fetch_row($result)) {
			$supplierList[$i]['sup_id']	= $row[0];
			$supplierList[$i]['sup_name']= $row[1];
			$supplierList[$i]['sup_address']= $row[2];
			$supplierList[$i]['sup_phone']= $row[3];
			$supplierList[$i]['sup_status']= $row[4];
			
			$i++;
		}
		return $supplierList;
	}
	
	function inactivateASupplier($sup_id){
		qdbconn();		
		$query = "UPDATE suppliers SET sup_status = 0 where sup_id = $sup_id";
		$result = mysql_query($query);
		return $result;
	}
	
	function activateASupplier($sup_id){
		qdbconn();		
		$query = "UPDATE suppliers SET sup_status = 1 where sup_id = $sup_id";
		$result = mysql_query($query);
		return $result;
	}
	
	function editASupplier($data){
		qdbconn();
		$sup_name 		= $data['sup_name'];
		$sup_id 		= $data['sup_id'];
		$sup_address 	= $data['sup_address'];
		$sup_phone		= $data['sup_phone'];
		$sup_status		= $data['sup_status'];
		
		$query = "SELECT count(1) FROM suppliers
					WHERE sup_name = '$sup_name' 
					and sup_address='$sup_address' and sup_phone='$sup_phone'";
		$result = mysql_query($query);
		if ($row = mysql_fetch_row($result)) {
			if ($row[0]==0) 
			{
				$query = "UPDATE suppliers SET sup_name='$sup_name', sup_address = '$sup_address',
								sup_phone = '$sup_phone' WHERE sup_id = $sup_id";
				
				$result = mysql_query($query);
				return $result;
			}
			else 
			{
				return -1; //duplicated
			}
		}			
	}
	
	function insertASupplier($data){
		qdbconn();
		$sup_name = $data['sup_name'];
		$sup_address = $data['sup_address'];
		$sup_phone = $data['sup_phone'];
		
		$query = "SELECT count(1) FROM suppliers
					WHERE sup_name = '$sup_name' and sup_address ='$sup_address' 
					and sup_phone='$sup_phone'";
		$result = mysql_query($query);
		if ($row = mysql_fetch_row($result)) {
			if ($row[0]==0) 
			{
				$query = "INSERT INTO suppliers (sup_name,sup_address,sup_phone) 
						values ('$sup_name','$sup_address','$sup_phone')";
				$result = mysql_query($query);
				return $result;
			}
			else 
			{
				return -1; //duplicated
			}
		}		
	}
	
	
	
	function getSuppliersInfo($sup_id){
		qdbconn();		
		$query = "SELECT * FROM suppliers
					WHERE sup_id = '$sup_id'";
		$result = mysql_query($query);
		$current_data = array();
		if ($row = mysql_fetch_row($result)) {
			$current_data['sup_id'] 		= $row[0];
			$current_data['sup_name'] 	= $row[1];
			$current_data['sup_address'] 		= $row[2];
			$current_data['sup_phone'] 	= $row[3];
			$current_data['sup_status'] 	= $row[4];
			
		}
		return $current_data;
	}
}

?>